package com.voyage.voyagemall.product.service.impl;

import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.voyage.voyagemall.common.utils.PageUtils;
import com.voyage.voyagemall.common.utils.Query;

import com.voyage.voyagemall.product.dao.CategoryDao;
import com.voyage.voyagemall.product.entity.CategoryEntity;
import com.voyage.voyagemall.product.service.CategoryService;


@Service("categoryService")
public class CategoryServiceImpl extends ServiceImpl<CategoryDao, CategoryEntity> implements CategoryService {


    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<CategoryEntity> page = this.page(
                new Query<CategoryEntity>().getPage(params),
                new QueryWrapper<CategoryEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public List<CategoryEntity> listWithTree() {
        /**
         * 1、查出所有分类
         * 2、组装成树形结构
         */
        List<CategoryEntity> entities = baseMapper.selectList(null);
        List<CategoryEntity> collect = entities.stream().filter(
                item -> item.getParentCid() == 0
        ).map((menu) -> {
            menu.setChildren(structChildren(menu, entities));
            return menu;
        }).sorted(Comparator.comparingInt(menu -> (menu.getSort() != null ? menu.getSort() : 0))).collect(Collectors.toList());
        return collect;
    }

    @Override
    public void batchRemoveByIds(List<Long> catIds) {
        //TODO  需要校验删除的逻辑
        baseMapper.deleteBatchIds(catIds);
        //最好采用逻辑删除

    }

    @Override
    public Long[] findCatelogPath(Long catelogId) {
        List<Long> paths = new ArrayList<>();
        List<Long> parentPath = findParentPath(catelogId, paths);
        Collections.reverse(parentPath);
        return parentPath.toArray(new Long[parentPath.size()]);

    }

    private List<Long> findParentPath(Long catelogId, List<Long> paths) {
        //收集当前节点id
        paths.add(catelogId);
        CategoryEntity byId = this.getById(catelogId);
        if (byId.getParentCid() != 0) {
            findParentPath(byId.getParentCid(), paths);
        }
        return paths;
    }


    /**
     * 递归组装树形结构
     *
     * @param root
     * @param all
     * @return
     */
    private List<CategoryEntity> structChildren(CategoryEntity root, List<CategoryEntity> all) {
        List<CategoryEntity> collect = all.stream().filter(categoryEntity -> {
            return categoryEntity.getParentCid() == root.getCatId();
        }).map(categoryEntity -> {
            categoryEntity.setChildren(structChildren(categoryEntity, all));
            return categoryEntity;
        }).sorted(Comparator.comparingInt(menu -> (menu.getSort() != null ? menu.getSort() : 0))).collect(Collectors.toList());
        return collect;
    }

}
